# 静态化
import sys

sys.path.insert(0, '../')

# 告诉django你的项目设置文件在哪
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")

# 导入配置文件
import django

django.setup()

from utils.goods import get_categories, get_breadcrumb, get_goods_specs
from apps.goods.models import SKU
from meiduo_mall.settings import BASE_DIR
import os


def generic_detail_html(sku_id):
    try:
        # SKU信息
        sku = SKU.objects.get(id=sku_id)
    except SKU.DoesNotExist:
        pass

    # 分类数据
    categories = get_categories()
    # 面包屑
    breadcrumb = get_breadcrumb(sku.category)
    # 规格信息
    good_specs = get_goods_specs(sku)

    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': good_specs,
    }
    # 加载模板
    from django.template import loader
    template_html = loader.get_template('detail.html')
    # 模板渲染
    template_html_data = template_html.render(context)
    file_path = os.path.join(os.path.dirname(BASE_DIR), "front_end_pc/goods/%s.html" % sku_id)
    # 写入指定文件
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(template_html_data)

    print(sku_id)


skus = SKU.objects.all()
for sku in skus:
    generic_detail_html(sku.id)
